package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsRecyclerApplyBo;
import com.kxmall.huishou.domain.vo.HsRecyclerApplyVo;
import com.kxmall.huishou.domain.HsRecyclerApply;
import com.kxmall.huishou.mapper.HsRecyclerApplyMapper;
import com.kxmall.huishou.service.IHsRecyclerApplyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 回收员申请信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-24
 */
@RequiredArgsConstructor
@Service
public class HsRecyclerApplyServiceImpl implements IHsRecyclerApplyService {

    private final HsRecyclerApplyMapper baseMapper;

    /**
     * 查询回收员申请信息
     */
    @Override
    public HsRecyclerApplyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询回收员申请信息列表
     */
    @Override
    public TableDataInfo<HsRecyclerApplyVo> queryPageList(HsRecyclerApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsRecyclerApply> lqw = buildQueryWrapper(bo);
        Page<HsRecyclerApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询回收员申请信息列表
     */
    @Override
    public List<HsRecyclerApplyVo> queryList(HsRecyclerApplyBo bo) {
        LambdaQueryWrapper<HsRecyclerApply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsRecyclerApply> buildQueryWrapper(HsRecyclerApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsRecyclerApply> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HsRecyclerApply::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), HsRecyclerApply::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), HsRecyclerApply::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), HsRecyclerApply::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), HsRecyclerApply::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getCountry()), HsRecyclerApply::getCountry, bo.getCountry());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), HsRecyclerApply::getIdCard, bo.getIdCard());
        lqw.eq(bo.getStatus() != null, HsRecyclerApply::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增回收员申请信息
     */
    @Override
    public Boolean insertByBo(HsRecyclerApplyBo bo) {
        HsRecyclerApply add = BeanUtil.toBean(bo, HsRecyclerApply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改回收员申请信息
     */
    @Override
    public Boolean updateByBo(HsRecyclerApplyBo bo) {
        HsRecyclerApply update = BeanUtil.toBean(bo, HsRecyclerApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsRecyclerApply entity){

//        //TODO 做一些数据校验,如唯一约束
//        // 如果发现错误
//        if (StringUtils.isNotBlank(entity.getUserId())) {
//            HsRecyclerApplyVo hsRecyclerApplyVo = baseMapper.selectVoByUserId(entity.getUserId());
//            if (hsRecyclerApplyVo!=null){
//                throw new ServiceException("你已注册，请不要重复注册！");
//            }
//        }
    }

    /**
     * 批量删除回收员申请信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean editByBo(HsRecyclerApplyBo bo) {
        HsRecyclerApply hsRecyclerApply = new HsRecyclerApply();
        hsRecyclerApply.setId(bo.getId());
        hsRecyclerApply.setStatus(bo.getStatus());

        int num = baseMapper.update(hsRecyclerApply, new QueryWrapper<HsRecyclerApply>().eq("id", bo.getId()));
        return num > 0;
    }

    @Override
    public HsRecyclerApplyVo queryByWrapper(QueryWrapper<HsRecyclerApply> queryWrapper) {
        return baseMapper.selectVoOne(queryWrapper);
    }
}
